import {
	observable,
	action,
	computed,
} from 'mobx';
import {
	WeaLocaleProvider,
} from 'ecCom';
import {message,Button} from 'antd';
import {isEmpty} from "lodash";
import {DialogStore} from '../public/valhalla/stores/index.js';
import FinanceExpand from './financeExpand.js';
import * as api from '../apis/payrollDetail.js';
const getLabel = WeaLocaleProvider.getLabel;

class HrmPayrollDetailBp{
	@observable loading = false;
	@observable notAuth = false;
	@observable firstCol = [];
	@observable otherCols = [];
	@observable data = [];
	@observable payrollDetail = {};

	@observable financeExpand = new FinanceExpand(this.init);

	@computed get hasPayrollDetail(){
		return !isEmpty(this.payrollDetail)
	}

	init = () => {
		this.getPayrollDetail();
	}

	getPayrollDetail = () => {
		this.loading = true;

		api.readRecord(this.params).then(datas => {
			const {data,status} = datas;
			this.notAuth = (status == '0');
			if (status ==='1') {
				this.payrollDetail = data;

				data && Object.keys(data).forEach(key => {
					this[key] = data[key];
				})

				this.splitData()
			}else{
				datas.message && message.warning(datas.message);
			}

			this.loading = false;
		})
	}

	splitData = () => {
		this.data && !isEmpty(this.data) && JSON.parse(this.data).forEach(item => {
			this.firstCol.push(item[0]);
			item.slice(1).map((val,index) => {
				if ( (this.otherCols.length -1) < index ) {
					this.otherCols.push([val]);
				}else{
					this.otherCols[index].push(val);
				}
			})
		})
	}

	setTargetId = (params) => {
		this.params = params;
	}

	get DialogButtons(){
		return [<Button ecId={`${this && this.props && this.props.ecId || ''}_Button@rg333o@${diffKey}`} type="primary" onClick={this.saveFeedback}>{getLabel(-1,"保存")}</Button>]
	} 

	@observable fbDialogStore = new DialogStore(this);

	confirmRecord = () => {
		api.confirmRecord({targetId: this.params.targetId}).then(res => {
			if (res.status == "1") {
				message.success(getLabel('524279','确定成功'));
				this.getPayrollDetail();
			}else{
				message.error(res.message);
			}
		})
	}

	feedback = () => {
	    const config = {
	        title: getLabel('524278','反馈信息'),
	        style: { height: 200 }
	    }
	    this.remark = "";
	    this.fbDialogStore.init(config);
	    this.fbDialogStore.openDialog();
	}

	handleTextareaChange = (text) => {
		this.remark = text;
	}

	saveFeedback = () => {
		api.feedback({targetId: this.params.targetId, remark: this.remark}).then(res => {
			if (res.status == "1") {
				message.success(getLabel('524280','信息已反馈'));
				this.getPayrollDetail();
				this.fbDialogStore.closeDialog();
			}else{
				message.error(res.message);
			}
		})
	}

}

export const hrmPayrollDetailBpStore = new HrmPayrollDetailBp();